$.namespace('mapPosition');
var icons = [ ctxStatic + '/map/images/1.png', ctxStatic + '/map/images/2.png',
		ctxStatic + '/map/images/3.png', ctxStatic + '/map/images/4.png',
		ctxStatic + '/map/images/5.png', ctxStatic + '/map/images/6.png',
		ctxStatic + '/map/images/7.png', ctxStatic + '/map/images/8.png',
		ctxStatic + '/map/images/9.png', ctxStatic + '/map/images/A.png',
		ctxStatic + '/map/images/B.png', ctxStatic + '/map/images/C.png',
		ctxStatic + '/map/images/D.png', ctxStatic + '/map/images/E.png',
		ctxStatic + '/map/images/F.png', ctxStatic + '/map/images/G.png',
		ctxStatic + '/map/images/H.png', ctxStatic + '/map/images/I.png',
		ctxStatic + '/map/images/J.png', ctxStatic + '/map/images/K.png',
		ctxStatic + '/map/images/L.png', ctxStatic + '/map/images/M.png',
		ctxStatic + '/map/images/O.png', ctxStatic + '/map/images/P.png',
		ctxStatic + '/map/images/Q.png', ctxStatic + '/map/images/R.png',
		ctxStatic + '/map/images/S.png', ctxStatic + '/map/images/T.png',
		ctxStatic + '/map/images/U.png', ctxStatic + '/map/images/V.png',
		ctxStatic + '/map/images/W.png', ctxStatic + '/map/images/X.png',
		ctxStatic + '/map/images/Y.png', ctxStatic + '/map/images/Z.png' ];
var typeImage = [];
var getImage = function(type) {
	for (var i = 0; i < typeImage.length; i++) {
		if (typeImage[i].type == type)
			return typeImage[i].image;
	}
	var len = typeImage.length;
	typeImage[len] = {
		type : type,
		image : icons[len]
	};
	return typeImage[len].image;

}
mapPosition = function() {
	return {
		load : function(map, data, editModel) {
			map.clearOverlays();
			var styleOptions = {
				strokeColor : "red", // 边线颜色。
				fillColor : "red", // 填充颜色。当参数为空时，圆形将没有填充效果。
				strokeWeight : 1, // 边线的宽度，以像素为单位。
				strokeOpacity : 0.8, // 边线透明度，取值范围0 - 1。
				fillOpacity : 0.3, // 填充的透明度，取值范围0 - 1。
				strokeStyle : 'solid' // 边线的样式，solid或dashed。
			};

			if (editModel == undefined)
				editModel = true;
			var editItemID = data.editItemID;
			if (editItemID == undefined)
				editItemID = '';

			for (var i = 0; i < data.overlays.length; i++) {
				var id = data.overlays[i].id;
				data.overlays[i].isedit = (id == editItemID && id != "");

				var stype = data.overlays[i].type;
				if (stype == undefined)
					continue;

				stype = stype.toUpperCase();
				var _overlay = undefined;
				if (stype == 'POINT') {
					var img = data.overlays[i].image;
					if (img == undefined)
						// img = ctxStatic + '/map/images/pin1.png';
						img = getImage(data.overlays[i].dataType);
					console.log(data.overlays[i].dataType);
					console.log(img);
					var myIcon = {};
					if (img != undefined)
						myIcon = {
							icon : new BMap.Icon(img, new BMap.Size(36, 36))
						};
					if (data.overlays[i].isedit) {
						myIcon = {
							// 指定Marker的icon属性为Symbol
							icon : new BMap.Symbol(BMap_Symbol_SHAPE_POINT, {
								scale : 1.5,// 图标缩放大小
								strokeColor : "yellow", // 边线颜色。
								fillColor : "red", // 填充颜色。当参数为空时，圆形将没有填充效果。
								strokeWeight : 1, // 边线的宽度，以像素为单位。
								strokeOpacity : 0.6, // 边线透明度，取值范围0 - 1。
								fillOpacity : 0.8, // 填充的透明度，取值范围0 - 1。
								strokeStyle : 'solid' // 边线的样式，solid或dashed。
							})
						}
					}

					var pt = data.overlays[i].point;

					if (pt.length < 2)
						try{
						pt = pt.split(pt, ',');
						}catch(e){
							pt=[0,0];
						}

					var point = new BMap.Point(pt[0], pt[1]);

					_overlay = new BMap.Marker(point, myIcon);

					if (data.overlays[i].text != undefined
							&& data.overlays[i].text != '') {
						var label = new BMap.Label(data.overlays[i].text, {
							offset : new BMap.Size(20, -10)
						});
						_overlay.setLabel(label);
					}
					if (data.overlays[i].isedit) {
						_overlay.setAnimation(BMAP_ANIMATION_BOUNCE);

						_overlay.addEventListener("dragging", function() {
							this.setAnimation();
						});
						_overlay.enableDragging();

						posMarker = _overlay;
					}
					_overlay.hide();
				} else if (stype == 'POLYGON' || stype == 'POLYLINE') {

					if (stype == 'POLYGON') {

						mapPosition.setPosition(map,
								(data.overlays[i].center[0]) + ","
										+ (data.overlays[i].center[1]), true);
					}

					var pts = data.overlays[i].points;
					var points = [];
					if (pts == undefined)
						pts = [];
					for (var j = 0; j < pts.length; j++) {
						points[j] = new BMap.Point(pts[j][0], pts[j][1]);
					}

					var style = styleOptions;
					if (data.overlays[i].strokeWeight != undefined)
						style.strokeWeight = data.overlays[i].strokeWeight;
					if (data.overlays[i].fillColor != undefined)
						style.fillColor = data.overlays[i].fillColor;
					if (data.overlays[i].strokeColor != undefined)
						style.strokeColor = data.overlays[i].strokeColor;

					if (style.strokeWeight == 0)
						style.strokeOpacity = 0.001;

					if (editModel) {
						style.strokeOpacity = 0.6;
						style.strokeWeight = 2;
						if (!data.overlays[i].isedit) {
							style.fillOpacity = 0.001;
							style.strokeStyle = 'dashed';
							style.fillColor = undefined;
							style.strokeColor = "blue";
						} else {
							style.fillColor = "red";
							style.fillOpacity = 0.2;
						}
					}

					_overlay = new BMap.Polygon(points, style);
					_overlay.isedit = data.overlays[i].isedit;
					if (data.overlays[i].center != undefined) {
						_overlay.center = new BMap.Point(
								data.overlays[i].center[0],
								data.overlays[i].center[1]);

					} else {
						if (pts.length > 0)
							_overlay.center = new BMap.Point(pts[0][0],
									pts[0][1]);
					}

					if (_overlay.isedit) {

						mapPosition.setPosition(map, (_overlay.center).lng
								+ "," + (_overlay.center).lat, true);
					}

					// 设置区域文本
					if (!editModel || (editModel && !data.overlays[i].isedit)) {
						var caption = data.overlays[i].text;
						var center = data.overlays[i].center;

						if (center != undefined) {

							var textPt = new BMap.Point(center[0], center[1]);
							var opts = {
								position : textPt, // 指定文本标注所在的地理位置
								offset : new BMap.Size(
										-caption.toString().length * 12 / 2,
										-10)
							// 设置文本偏移量
							};
							var label = new BMap.Label(caption, opts); // 创建文本标注对象
							label.setStyle({
								color : "red",
								fontSize : "12px",
								height : "20px",
								lineHeight : "20px",
								fontFamily : "微软雅黑"
							});
							_overlay.label = label;
							map.addOverlay(label);
						}
					}
				}
				if (_overlay != undefined) {
					_overlay.isedit = data.overlays[i].isedit;
					_overlay.dataType = data.overlays[i].dataType;
					_overlay.id = data.overlays[i].id;
					_overlay.messageData = data.overlays[i].messageData;

					_overlay.addEventListener("click", function(e) {

						mapPosition.showMessage(this, e.point);
					});
					if (_overlay.id == undefined)
						_overlay.id = 'overlay_' + i;
					m_overlays.push(_overlay);
					map.addOverlay(_overlay);

					if (_overlay.isedit && editModel) {
						_overlay.enableEditing();
						drawOverlay = _overlay;
					}
				}
			}

		},
		init : function(id, pt) {

			// 百度地图API功能
			var map = new BMap.Map(id, {
				enableMapClick : false
			});// 构造底图时，关闭底图可点功能
			map.enableScrollWheelZoom(); // 启用滚轮放大缩小

			var overView = new BMap.OverviewMapControl();
			var mapType1 = new BMap.MapTypeControl({
				anchor : BMAP_ANCHOR_TOP_RIGHT,
				mapTypes : [ BMAP_NORMAL_MAP, BMAP_HYBRID_MAP ]
			});
			map.addControl(mapType1); // 2D图，卫星图
			map.addControl(overView); // 添加默认缩略地图控件

			var point = new BMap.Point(119.612791, 32.01617);
			map.centerAndZoom(point, 16);

			return map;
		},
		initDrawmode : function(mode) {

			// ----------------------------
			// 绘制控件
			// ----------------------------
			var drawMode = [];
			if (mode == 'POLYGON')
				drawMode = [ BMAP_DRAWING_POLYGON ];
			else if (mode == 'POLYLINE')
				drawMode = [ BMAP_DRAWING_POLYLINE ];
			else if (mode == 'POINT')
				drawMode = [ BMAP_DRAWING_MARKER ];

			if (drawMode.length > 0) {
				var styleOptions = {
					strokeColor : "red", // 边线颜色。
					fillColor : "red", // 填充颜色。当参数为空时，圆形将没有填充效果。
					strokeWeight : 2, // 边线的宽度，以像素为单位。
					strokeOpacity : 0.8, // 边线透明度，取值范围0 - 1。
					fillOpacity : 0.4, // 填充的透明度，取值范围0 - 1。
					strokeStyle : 'solid' // 边线的样式，solid或dashed。
				};
				// 实例化鼠标绘制工具
				var drawingManager = new BMapLib.DrawingManager(map, {
					isOpen : false, // 是否开启绘制模式
					enableDrawingTool : true, // 是否显示工具栏
					drawingToolOptions : {
						anchor : BMAP_ANCHOR_TOP_LEFT, // 位置
						offset : new BMap.Size(450, 8), // 偏离值
						scale : 0.6,
						drawingModes : drawMode
					},
					circleOptions : styleOptions, // 圆的样式
					polylineOptions : styleOptions, // 线的样式
					polygonOptions : styleOptions, // 多边形的样式
					rectangleOptions : styleOptions
				// 矩形的样式
				});
				// 添加鼠标绘制工具监听事件，用于获取绘制结果
				drawingManager.addEventListener('overlaycomplete', function(e) {
					if (drawOverlay != undefined)
						map.removeOverlay(drawOverlay);
					drawOverlay = e.overlay;
					drawOverlay.enableEditing();
					drawOverlay.bEdit = true;

				});

			}

		},
		initSearchBar : function() {
			// ----------------------------
			// 自动完成
			// ----------------------------
			var ac = new BMap.Autocomplete( // 建立一个自动完成的对象
			{
				"input" : "suggestId",
				"location" : map
			});

			ac.addEventListener("onhighlight", function(e) { // 鼠标放在下拉列表上的事件
				var str = "";
				var _value = e.fromitem.value;
				var value = "";
				if (e.fromitem.index > -1) {
					value = _value.province + _value.city + _value.district
							+ _value.street + _value.business;
				}
				str = "FromItem<br />index = " + e.fromitem.index
						+ "<br />value = " + value;

				value = "";
				if (e.toitem.index > -1) {
					_value = e.toitem.value;
					value = _value.province + _value.city + _value.district
							+ _value.street + _value.business;
				}
				str += "<br />ToItem<br />index = " + e.toitem.index
						+ "<br />value = " + value;
				$("#searchResultPanel").innerHTML = str;
			});

			var myValue;
			ac.addEventListener("onconfirm", function(e) { // 鼠标点击下拉列表后的事件
				var _value = e.item.value;
				myValue = _value.province + _value.city + _value.district
						+ _value.street + _value.business;
				$("#searchResultPanel").innerHTML = "onconfirm<br />index = "
						+ e.item.index + "<br />myValue = " + myValue;

				function myFun() {
					var p = local.getResults().getPoi(0).point;
					mapPosition.setPosition(map, p.lng + "," + p.lat, true);
				}
				var local = new BMap.LocalSearch(map, { // 智能搜索
					onSearchComplete : myFun
				});
				local.search(myValue);
			});
		},
		initLayerBar : function(map, layers) {
			$("#divMapDisplayLayer").remove();
			if (layers == undefined)
				return;
			if (layers.length == 0)
				return;
			// 通过JavaScript的prototype属性继承于BMap.Control
			mapPosition.ZoomControl.prototype = new BMap.Control();
			// 自定义控件必须实现自己的initialize方法,并且将控件的DOM元素返回
			// 在本方法中创建个div元素作为控件的容器,并将其添加到地图容器中
			mapPosition.ZoomControl.prototype.initialize = function(map) {

				// 创建一个DOM元素
				var div = document.createElement("div");
				div.setAttribute("id", "divMapDisplayLayer");
				// 添加DOM元素到地图中
				map.getContainer().appendChild(div);
				// 将DOM元素返回
				mapPosition.addMaplayers(layers);
				return div;
			};
			// 创建控件
			var myZoomCtrl = new mapPosition.ZoomControl();
			// 添加到地图当中
			map.addControl(myZoomCtrl);
		},
		initGridBar : function(map, grids) {

			if (grids == undefined)
				return;
			if (grids.length == 0)
				return;
			// 通过JavaScript的prototype属性继承于BMap.Control
			mapPosition.GridControl.prototype = new BMap.Control();
			// 自定义控件必须实现自己的initialize方法,并且将控件的DOM元素返回
			// 在本方法中创建个div元素作为控件的容器,并将其添加到地图容器中

			mapPosition.GridControl.prototype.initialize = function(map) {

				$("#divGrid").remove();
				// 创建一个DOM元素
				var div = document.createElement("div");
				div.setAttribute("id", "divGrid");
				// 添加DOM元素到地图中
				map.getContainer().appendChild(div);

				// 将DOM元素返回
				mapPosition.addGrids(grids);
				return div;
			};
			// 创建控件
			var GridControl = new mapPosition.GridControl();

			// 添加到地图当中
			map.addControl(GridControl);
		},
		// 显示与关闭图层
		showLayer : function(type, bshow) {
			// console.log('----------------------------------');
			// console.log( type);
			// console.log('----------------------------------');
			for (var i = 0; i < m_overlays.length; i++) {
				// console.log(m_overlays[i].dataType);
				if (m_overlays[i].dataType == type) {
					if (bshow) {
						m_overlays[i].show();
						if (m_overlays[i].label != undefined)
							m_overlays[i].label.show()
					} else {
						m_overlays[i].hide();
						if (m_overlays[i].label != undefined)
							m_overlays[i].label.hide()
					}
				}
			}
		},
		setPosition : function(map, pt, anchor) {
			if (pt == undefined || pt == "")
				pt = "119.612791,32.01617";
			var sPoints = pt.split(",");
			point = new BMap.Point(sPoints[0], sPoints[1]);
			map.centerAndZoom(point, map.getZoom());
			if (anchor) {
				if (posMarker == undefined) {
					posMarker = new BMap.Marker(point, {
						// 指定Marker的icon属性为Symbol
						icon : new BMap.Symbol(BMap_Symbol_SHAPE_POINT, {
							scale : 1.5,// 图标缩放大小
							strokeColor : "yellow", // 边线颜色。
							fillColor : "red", // 填充颜色。当参数为空时，圆形将没有填充效果。
							strokeWeight : 1, // 边线的宽度，以像素为单位。
							strokeOpacity : 0.6, // 边线透明度，取值范围0 - 1。
							fillOpacity : 0.8, // 填充的透明度，取值范围0 - 1。
							strokeStyle : 'solid' // 边线的样式，solid或dashed。
						})
					});// 创建标注
					map.addOverlay(posMarker); // 将标注添加到地图中
					posMarker.enableDragging();

					posMarker.addEventListener("dragging", function() {
						this.setAnimation()
					});
					posMarker.setAnimation(BMAP_ANIMATION_BOUNCE); // 跳动的动画
					posMarker.disableMassClear();
				} else {
					posMarker.setPosition(point);
				}
			}
		},
		getPosition : function() {
			var pt = posMarker.getPosition();
			return pt.lng + "," + pt.lat;
		},
		getRegion : function() {
			var ret = {};
			ret.type = "POLYGON";
			ret.points = [];

			if (drawOverlay instanceof BMap.Polyline) {
				ret.type = "POLYLINE";
			}
			if (drawOverlay instanceof BMap.Polygon) {
				ret.type = "POLYGON";
			}
			if (drawOverlay instanceof BMap.Point) {
				ret.type = "POINT";
			}

			var pts = drawOverlay.getPath();

			for (var i = 0; i < pts.length; i++) {
				ret.points.push([ pts[i].lng, pts[i].lat ]);
			}
			return JSON.stringify(ret);
		},
		// 定义一个控件类,即function
		ZoomControl : function() {
			// 默认停靠位置和偏移量
			this.defaultAnchor = BMAP_ANCHOR_TOP_RIGHT;
			this.defaultOffset = new BMap.Size(110, 10);
		},
		// 定义一个控件类,即function
		GridControl : function() {
			// 默认停靠位置和偏移量
			this.defaultAnchor = BMAP_ANCHOR_TOP_LEFT;
			this.defaultOffset = new BMap.Size(5, 10);
		},
		// 增加图层控制内容
		addMaplayers : function(layers) {

			var stable = "<table id='layeritems' style='font-family: 微软雅黑;font-size: 12px;'>[ROWS]</table>";
			var srows = "";
			for (var i = 0; i < layers.length; i++) {
				// console.log(layers[i]);
				layers[i].checked = 'true';
				layers[i].checked = 'false';

				var schecked = '';
				if (layers[i].checked == 'true')
					schecked = "checked='checked'";
				/*
				 * srows = srows + "<tr><td style='width:30px;'><img src='" +
				 * ctxStatic + layers[i].image + "'></img></td>" + "<td style='width:100px;'>" +
				 * layers[i].name + "</td>" + "<td> <input type='checkbox'
				 * id='CBX_" + layers[i].code + "' " + schecked + " onclick =
				 * \"mapPosition.showLayer('" + layers[i].code +
				 * "',this.checked);\"/></td></tr>";
				 */
				srows = srows + "<tr>"
						+ "<td><img style='width:24px;height:24px' src='"
						+ getImage(layers[i].type)
						+ "'/></td> <td> <input type='checkbox' id='CBX_"
						+ layers[i].type + "' " + schecked
						+ " onclick = \"mapPosition.showLayer('"
						+ layers[i].type + "',this.checked);\"/></td>"
						+ "<td style='width:90px;'>" + layers[i].name + "</td>";
				i++;
				if (i < layers.length) {
					srows = srows
							+ "<td><img style='width:24px;height:24px' src='"
							+ getImage(layers[i].type)
							+ "'/></td> <td> <input type='checkbox' id='CBX_"
							+ layers[i].type + "' " + schecked
							+ " onclick = \"mapPosition.showLayer('"
							+ layers[i].type + "',this.checked);\"/></td>"
							+ "<td style='width:90px;'>" + layers[i].name
							+ "</td>"
				} else {
					srows = srows + "<td colspan=3></td>";
				}
				srows = srows + "</tr>";

			}

			stable = stable.replace("[ROWS]", srows);
			var div = document.createElement("div");
			div.innerHTML = "<span style='cursor:pointer;font-weight:bold;color:blue' id='LayerButton' onclick='if($(\"#layeritems\").is(\":hidden\")) {$(\"#layeritems\").show();this.innerHTML=\"[隐藏]\";} else{$(\"#layeritems\").hide();this.innerHTML=\"[显示]\";}'>[隐藏]</span>";
			$("#divMapDisplayLayer").append(div);
			// $("#LayerButton").css('cursor','point');

			div = document.createElement("div");
			div.innerHTML = stable;
			$("#divMapDisplayLayer").append(div);

		},
		addGrids : function(grids) {

			var schecked = 'checked="checked"';
			var stable = "<table id='ctlgrids' style='font-family: 微软雅黑;font-size: 12px;'>[ROWS]</table>";
			var srows = "";
			for (var i = 0; i < grids.length; i++) {

				srows = srows + "<tr>"
						+ "<td> <input type='radio' name='sq' id='RD_"
						+ grids[i].id + "' " + schecked
						+ " onclick = \"getLayers(mapPosition.initLayerBar,'"
						+ grids[i].id + "');getData(mapPosition.load,'"
						+ grids[i].id + "');\"/></td>"
						+ "<td style='width:90px;'>" + grids[i].name + "</td>";
				srows = srows + "</tr>";

				schecked = '';

			}

			stable = stable.replace("[ROWS]", srows);
			var div = document.createElement("div");
			div.innerHTML = "<span style='cursor:pointer;font-weight:bold;color:blue' id='SQButton' onclick='if($(\"#ctlgrids\").is(\":hidden\")) {$(\"#ctlgrids\").show();this.innerHTML=\"[隐藏]\";} else{$(\"#ctlgrids\").hide();this.innerHTML=\"[显示]\";}'>[隐藏]</span>";
			$("#divGrid").append(div);
			div = document.createElement("div");
			div.innerHTML = stable;
			$("#divGrid").append(div);

			if (grids.length > 0) {
				// 加载网格图层与元素
				getLayers(mapPosition.initLayerBar, grids[0].id);
				getData(mapPosition.load, grids[0].id);
			}
		},
		showMessage : function(e, p) {

			// e.messageData="adsfdsafdfa";
			if (e.messageData == undefined) {

				if (e.url == undefined || e.url == '')
					return;
				// ajax request
			}
			if (e.messageData == undefined)
				return;

			var msg = this.GenMessager(e.dataType, e.messageData);

			var infoWindow = new BMap.InfoWindow(msg);
			if (e instanceof BMap.Polyline || e instanceof BMap.Polygon) {
				if (p == undefined)
					p = this.getPath()[0];
				map.openInfoWindow(infoWindow, p); // 开启信息窗口
			} else {
				e.openInfoWindow(infoWindow);
			}
		},
		// 根据类型生成信息框内容
		GenMessager : function(dataType, data) {

			if (dataType == undefined)
				dataType = '1';
			function genContentGrid(data) {
				var sret = "<table style='width:250px; font-size: 12px;'><th colspan=2 style='background:#00f; color:#fff; font-size: 14px;height:32px;text-align:center'><B>广场信息</B></th>"
						+ "<tr style='height:26px'><td style='width:80px'>广场编号：</td><td>"
						+ data.code
						+ "</td></tr>"
						+ "<tr  style='height:26px'><td style='width:80px'>广场名称：</td><td>"
						+ data.name
						+ "</td></tr>"
						+ "<tr  style='height:26px'><td style='width:80px'>广场功能：</td><td>"
						+ data.remark + "</td></tr>" + "</table>";
				return sret;
			}
			function genContentSite(data) {
				console.log(data);
				
				var imgsrc = data.image;
				if (imgsrc == undefined || imgsrc == '' || imgsrc == null)
					imgsrc = ctxStatic + '/map/images/default.jpg';
				else
					imgsrc = ctxRest + '/sys/file/get?path=' + imgsrc;
				
			 
				var sret = "<table style='width:400px; font-size: 12px;'><th colspan=3 style='background:#00f; color:#fff; font-size: 14px;height:32px;text-align:center'><B>设施信息</B></th>"
						+ "<tr style='height:26px'><td rowspan=5 style='width:130px'><img style='width:100px;height:120px;border:1px solid #00f' src='"
						+ imgsrc
						+ "'/></td>"
						+ "<td>广场名称</td><td>"
						+ data.gridName
						+ "</td></tr>"
						+ "<tr  style='height:26px'><td>广场区域</td><td>"
						+ data.regionName
						+ "</td></tr>"
						+ "<tr  style='height:26px'><td style='width:80px'>设施编号：</td><td>"
						+ data.code
						+ "</td></tr>"
						+ "<tr style='height:26px'><td style='width:80px'>设施名称：</td><td>"
						+ data.name
						+ "</td></tr>"
						+ "<tr style='height:26px'><td style='width:80px'>设施功能：</td><td>"
						+ data['function'] + "</td></tr>" + "</table>";
				return sret;
			}

			if (dataType == "0")
				return genContentGrid(data);

			else
				return genContentSite(data);

			// return dataType;
		}
	};
}();

var map = undefined;
var posMarker = undefined;
var drawOverlay = undefined;

// 数据加载函数
var loadData = undefined;
// 加载对象
var m_overlays = []; // 当前加载的Overlays集合

$(function() {
	// alert( window.location.href );
	// 初始化地图
	map = mapPosition.init('allmap', ipt);

	$("#info").hide();
	// $("#info").show();
	// mapPosition.initSearchBar();

	// 取修改或显示模式
	var bedit = false;
	bedit = (getArgsFromHref("edit") == 1);

	// 设置形状绘制控件
	var drawMode = getArgsFromHref('drawMode').toUpperCase();
	mapPosition.initDrawmode(drawMode);

	// 取父窗口传入的回调函数
	var loadFunction = getArgsFromHref("load");

	if (loadFunction != undefined && loadFunction != "") {
		// loadFunction ='getTestData';
		eval("loadData=" + loadFunction);
	}

	if (loadData != undefined) {
		id = getArgsFromHref("id");

		var s = loadData(id);
		mapPosition.load(map, loadData(id));
	}

	// 
	var loadGrids = getArgsFromHref("loadGrids");
	if (loadGrids != undefined && loadLayer != "") {
		eval(loadGrids + '(mapPosition.initGridBar);');
	}

	// 生成图层开关
	var loadLayer = getArgsFromHref("loadLayer");
	// loadLayer = 'getLayers';
	if (loadLayer != undefined && loadLayer != "") {
		eval(loadLayer + '(mapPosition.initLayerBar);');
	}

	var loadView = getArgsFromHref("loadView");
	// loadView = 'getData';
	if (loadView != undefined && loadView != "") {
		eval(loadView + '(mapPosition.load);');
	}

	// 设置中心点
	var ipt = getArgsFromHref("center");
	if (ipt != undefined && ipt != "")
		mapPosition.setPosition(map, ipt, false);

	// 设置位置点
	ipt = getArgsFromHref("pos");
	// if (ipt != undefined && ipt != "")
	mapPosition.setPosition(map, ipt, true);
})

// /////////////////////////////////////
// 取参数函数
// ////////////////////////////////////
function getArgsFromHref(sArgName) {
	var sHref = window.location.href;
	var args = sHref.split("?");
	var retval = "";
	if (args[0] == sHref) /* 参数为空 */
	{
		return retval; /* 无需做任何处理 */
	}
	var str = args[1];
	args = str.split("&");
	for (var i = 0; i < args.length; i++) {
		str = args[i];
		var arg = str.split("=");
		if (arg.length <= 1)
			continue;
		if (arg[0].toUpperCase() == sArgName.toUpperCase())
			retval = arg[1];
	}
	return retval;
}

function getGrids(f) {

	$.ajax({
		type : 'POST',
		url : ctxRest + '/gcb/grid/findList',
		contentType : 'application/json;charset=UTF-8',
		success : function(data) {
			f(map, data);
		}
	})
}

// 设施
function getLayers(f, id) {
	$.ajax({
		type : 'GET',
		// url : ctxRest + '/sys/basecode/get?classCode=设施类型',
		url : ctxRest + '/gcb/facility/findRegionTypesList?id=' + id,
		contentType : 'application/json;charset=UTF-8',
		success : function(data) {
			f(map, data);
		}
	})
}

// ply内容
function getData(f, id) {

	$.ajax({
		type : 'GET',
		url : ctxRest + '/grid/map/list?id=' + id,
		contentType : 'application/json;charset=UTF-8',
		success : function(data) {
			f(map, data, false);
		}
	})
}